<template>
  <div class="flexBox">
    <div id="Goods">
      <div v-for="item in dataList" :key="item.name" class="goodsBox">
        <img :src="require('../assets/img/'+item.name+'.jpg')">
        <div class="goodsInfo">
          <p class="phone">{{item.name}}</p>
          <P class="stock">
            库存: <span v-show="!item.isShow">{{item.newStock}}</span>
            <input type="text" v-model="item.newStock" v-if="item.isShow">
            <i v-show="!item.isShow" class="el-icon-edit" @click="changeShow(item.name)"></i>
            <i v-show="item.isShow" class="el-icon-circle-check" @click="changeShow(item.name);commit(item.newStock,item.name)"></i>
          </P>
          <p class="price">￥{{item.price}}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getGoodsData,editStock } from '../../api/data'
export default {
  name: 'Goods',
  data() {
    return {
      dataList: [],
    }
  },
  methods: {
    //切换输入框出现和隐藏
    changeShow(Name) {
      this.dataList.forEach((item) => {
        if (item.name == Name) {
          item.isShow = !item.isShow;
        }
      })
    },
    //提交更改
    commit(newStock,name){
      editStock(newStock,name).then((res)=>{
        let {code} = res.data;
        if(code == 999){
          console.log('修改成功');
        }else{
          this.dataList.forEach((item) => {
            if (item.name == name) {
              item.newStock = item.stock;
            }
          })
        }
      })
    }
  },
  mounted() {
    getGoodsData().then((res) => {
      const { code, data } = res.data;
      if (code === 999) {
        data.forEach((item) => {
          item.isShow = false;
          item.newStock = item.stock;
        })
        this.dataList = data;
      }
    })
  }
}
</script>

<style lang="less" scoped>
i {
  font-size: 14px;
  margin-top: 5px;
  cursor: pointer;
}

i:hover {
  color: rgb(72, 162, 255);
}

img {
  height: 200px;
  width: 180px;
  margin-left: 10px;
}

p {
  line-height: 25px
}

.flexBox {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-content: center;
}

#Goods {
  display: flex;
  justify-content: space-between;
  align-content: space-around;
  flex-shrink: 0;
  flex-basis: 1316px;
  height: 691px;
  flex-wrap: wrap;

  .goodsBox {
    display: flex;
    flex-direction: column;
    align-content: space-between;
    width: 200px;
    height: 300px;
    background-color: rgb(255, 255, 255);
    box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
    border-radius: 4px;
    transition: all .25s ease-in;

    .goodsInfo {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      margin: 15px 0 0 10px;
      font-weight: bold;
      color: rgb(70, 70, 72);

      .phone {
        display: flex;
        width: 180px;
        justify-content: space-between;
        font-size: 18px;
      }

      .stock {
        margin-top: 5px;
        font-size: 13px;
        color: rgb(131, 135, 147);

        input {
          width: 50px;
          padding-left: 5px;
        }
        i{
          margin-left: 5px;
        }
      }

      .price {
        display: flex;
        justify-content: space-between;
        margin-left: 102px;
        width: 75px;
        font-size: 22px;
        color: rgb(37, 56, 75);
      }
    }
  }

  .goodsBox:hover {
    transform: translateY(-8px);
    box-shadow: 0 12px 22px 0 rgb(0 0 0 / 30%);
  }
}
</style>